<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/25
 * Time: 15:44
 */
namespace App\Http\Controllers\admin_system;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Module\EntManageuser;
use App\Module\EntPosterTemplate;
use Cookie;
use DB;
use Validator;
use Redirect;
use Session;

class PosterController extends Controller
{
    public function index($keyword = '')
    {
        if ($keyword != '') {
            $posterTemplate = DB::table('PosterTemplate')->select('PoTeID', 'PosterTitle', 'ReleaseDate', 'IsShow', 'AddDate')
                ->where('DeleteMark', '1')
                ->where(function ($query) use ($keyword) {
                    $query->orwhere('PosterTitle', 'like', '%' . $keyword . '%');
                })
                ->orderBy('ReleaseDate', 'desc')
                ->paginate(20);
        } else {
            $posterTemplate = DB::table('PosterTemplate')->select('PoTeID', 'PosterTitle', 'ReleaseDate', 'IsShow', 'AddDate')
                ->where('DeleteMark', '1')
                ->orderBy('ReleaseDate', 'desc')
                ->paginate(20);
        }
        return view('admin_system.poster.index', [
            'posterTemplate' => $posterTemplate,
            'keyword' => $keyword
        ]);
    }


    public function createindex()
    {
        return view('admin_system.poster.indexAdd');
    }

    public function storeindex(Request $request)
    {
        $result = '';
        $AName = Session::get('AName');
        $AKey = Session::get('AKey');

        if ($AKey != '' && $AName != '') {
            $entManageuser = EntManageuser::select('UserID')->where('UserName', $AName)
                ->where('UserPwd', md5($AKey))
                ->where('DeleteMark', 1)
                ->first();
        }
        if ($entManageuser) {
            DB::beginTransaction();
            try {
                $entPosterTemplate = new EntPosterTemplate();
                $entPosterTemplate->PosterTitle = trim($request->get('txtPosterTitle'));
                $FilePath1 = '';
                if ($request->get('File_1') != '') {
                    $FilePath1 = $request->get('File_1');
                }
                $entPosterTemplate->FilePath1 = $FilePath1;
                $entPosterTemplate->IsShow = trim($request->get('RadIsShow'));
                $entPosterTemplate->MasterID = $entManageuser->UserID;
                $entPosterTemplate->DeleteMark = 1;
                $entPosterTemplate->ReleaseDate = date('Y-m-d H:i:s', time());
                $entPosterTemplate->AddDate = date('Y-m-d H:i:s', time());
                $entPosterTemplate->save();
                DB::commit();
            } catch (Exception $e) {
                DB::rollBack();
            }
            if ($entPosterTemplate) {
                $result = '模板添加成功！';
            } else {
                $result = '模板添加失败！';
            }
        } else {
            $result = '模板添加失败！';
        }
        return redirect::back()->withInput()->withErrors($result);
    }
    public function indexedit($id)
    {
        try {
            $entPosterTemplate = EntPosterTemplate::findOrFail($id);
        } catch (ModelNotFoundException $e) {
            throw new NotFoundHttpException();
        }
        return view('admin_system.poster.indexEdit', ['entPosterTemplate' => $entPosterTemplate]);
    }
    public function indexupdate(Request $request,$id){
        $result = '';
        $AName = Session::get('AName');
        $AKey = Session::get('AKey');

        if ($AKey != '' && $AName != '') {
            $entManageuser = EntManageuser::select('UserID')->where('UserName', $AName)
                ->where('UserPwd', md5($AKey))
                ->where('DeleteMark', 1)
                ->first();
        }
        if ($entManageuser) {
            DB::beginTransaction();
            try {
                $entPosterTemplate = EntPosterTemplate::findOrFail($id);
                $entPosterTemplate->PosterTitle = trim($request->get('txtPosterTitle'));
                $FilePath1 = '';
                if ($request->get('File_1') != '') {
                    $FilePath1 = $request->get('File_1');
                }
                $entPosterTemplate->FilePath1 = $FilePath1;
                $entPosterTemplate->IsShow = trim($request->get('RadIsShow'));
                $entPosterTemplate->MasterID = $entManageuser->UserID;
                $entPosterTemplate->ReleaseDate = date('Y-m-d H:i:s', time());
                $entPosterTemplate->save();
                DB::commit();
            } catch (Exception $e) {
                DB::rollBack();
            }
            if ($entPosterTemplate) {
                $result = '模板修改成功！';
            } else {
                $result = '模板修改失败！';
            }
        } else {
            $result = '模板修改失败！';
        }
        return redirect::back()->withInput()->withErrors($result);
    }
    public function indexdelete(Request $request)
    {
        $result = '';
        try {
            if ($request->ajax()) {
                $IDList = $request->json('IDList');
                if (is_numeric($IDList)) {
                    EntPosterTemplate::where('PoTeID', $IDList)->update([
                        'DeleteMark' => '0'
                    ]);
                    $result = 'ok';
                } else {
                    EntPosterTemplate::whereIn('PoTeID', explode(',', $IDList))->update([
                        'DeleteMark' => '0'
                    ]);
                    $result = 'ok';
                }
            }
        } catch (Exception $e) {
            $result = 'no';
        }
        return $result;
    }
}